package com.chd.ledger_server.controller;

import com.chd.ledger_server.dao.DevLedgerDao;
import com.chd.ledger_server.entity.DevLedger;
import com.chd.ledger_server.entity.VHotLedgerCard;
import com.chd.ledger_server.service.DevLedgerService;
import com.chd.ledger_server.domain.PageDto;
import com.chd.ledger_server.domain.Result;
import com.chd.ledger_server.service.VHotLedgerCardService;
import com.chd.ledger_server.service.VLedgerComprehensiveService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备检修台账表(DevLedger)表控制层
 *
 * @author makejava
 * @since 2025-07-25 15:27:11
 */
@RestController
@RequestMapping("devLedger")
@CrossOrigin
public class DevLedgerController {
    /**
     * 服务对象
     */
    @Resource
    private DevLedgerService devLedgerService;

    @Resource
    private VLedgerComprehensiveService vLedgerComprehensiveService;

    @Resource
    private VHotLedgerCardService vHotLedgerCardService;

    @Resource
    private DevLedgerDao devLedgerDao;

    /**
     * 分页查询台账实体
     *
     * @param pageDto 分页请求实体，仅包含DevLedger实体+分页数据
     * @return DevLedger列表
     */
    @PostMapping("/queryByPage")
    public Result queryByPage(@RequestBody PageDto pageDto) {
        PageInfo list = devLedgerService.queryAllDevLedgersByPage(pageDto);
        if (list.getList().isEmpty()) return Result.buildFail("未查到相关信息！");
        return Result.buildSuccess(list);
    }

    /**
     * 分页查询台账视图实体
     */
    @PostMapping("/queryViewsByPage")
    public Result queryViewsByPage(@RequestBody PageDto pageDto){
        PageInfo list = vLedgerComprehensiveService.queryAllVLedgerComprehensivesByPage(pageDto);
        if(list.getList().isEmpty())return Result.buildFail("未查询到相关信息！");
        return Result.buildSuccess(list);
    }

    /**
     * 通过台账id查询模型热点台账信息卡片视图
     * @param ledId，台账id
     * @return 模型热点台账信息卡片视图实体
     */
    @GetMapping("/queryCardById")
    public Result queryCardById(Integer ledId){
        VHotLedgerCard result = vHotLedgerCardService.queryById(ledId);
        if(result == null)return Result.buildFail("暂无历史台账");
        return Result.buildSuccess(result);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryById")
    public Result queryById(Integer id) {
        DevLedger entity = this.devLedgerService.queryById(id);
        if (entity == null) {
            return Result.buildFail("未找到记录");
        }
        return Result.buildSuccess(entity);
    }

    /**
     * 根据设备ID查询状态为2且检查日期最新的台账ID
     * @param deviceId 设备ID
     * @return 台账ID（ledId）
     */
    @GetMapping("/queryLatestLedgerId")
    public Result queryLatestLedgerId(Integer deviceId) {
        if (deviceId == null) {
            return Result.buildFail("设备ID不能为空");
        }

        // 构造查询条件：设备ID和状态为2
        DevLedger queryLedger = new DevLedger();
        queryLedger.setDeviceId(deviceId);
        queryLedger.setLedStatus(2);

        // 查询符合条件的台账
        List<DevLedger> ledgers = devLedgerDao.queryAll(queryLedger);
        if (ledgers.isEmpty()) {
            return Result.buildFail("未找到状态为2的设备台账记录");
        }

        // 按检查日期倒序排序，取最新的一条
        DevLedger latestLedger = ledgers.stream()
                .max((l1, l2) -> l1.getCheckDate().compareTo(l2.getCheckDate()))
                .orElse(null);

        if (latestLedger == null) {
            return Result.buildFail("未找到有效的台账记录");
        }

        return Result.buildSuccess(latestLedger.getLedId());
    }

    /**
     * 新增数据
     *
     * @param devLedger 实体
     * @return 新增结果
     */
    @PostMapping("/addOne")
    public Result addOne(@RequestBody DevLedger devLedger) {
        DevLedger result = this.devLedgerService.insert(devLedger);
        return Result.buildSuccess(result);
    }

    /**
     * 新增数据
     *
     * @param devLedgerList 实体列表
     * @return 批量新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody List<DevLedger> devLedgerList) {
        String result = this.devLedgerService.insertBatch(devLedgerList);
        return Result.buildSuccess(result);
    }

    /**
     * 编辑数据
     *
     * @param devLedger 实体
     * @return 编辑结果
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody DevLedger devLedger) {
        DevLedger result = this.devLedgerService.update(devLedger);
        if (result == null) {
            return Result.buildFail("更新失败，记录不存在");
        }
        return Result.buildSuccess(result);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/deleteById")
    public Result deleteById(Integer id) {
        boolean success = this.devLedgerService.deleteById(id);
        if (success) {
            return Result.buildSuccess("删除成功");
        } else {
            return Result.buildFail("删除失败，记录不存在");
        }
    }
}
